
//
//  KeChengXueNextWoDeZiYuanView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/17.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "KeChengXueNextWoDeZiYuanView.h"
#import "SYRequestAPI+Teach.h"
#import "SYPPTViewController.h"
#import "SYPlayerViewController.h"


@interface KeChengXueNextWoDeZiYuanView ()

@property (strong, nonatomic) UILabel *titleLab;

@end


@implementation KeChengXueNextWoDeZiYuanView
- (instancetype)initWithClassID:(NSString *)classID andLessonID:(NSString *)lessonID attendProgressId:(NSString *)attendProgressId withFrame:(CGRect )rect{
    if ([super initWithFrame:rect]) {
        self.classID = classID;
        self.lessonId = lessonID;
        self.attendProgressId = attendProgressId;
        [self initSubView];
    }
    return self;
}
- (void)initSubView{
    _dataArray = [NSMutableArray new];
    [self addSubview:self.tableList];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    
//   NSString *path = @"gjoqrEbAGTTXCxq/5dYm7rxePwHLvvb9g5uVAUV/QoxR6Wxntzr6Ja219ww0istuwPJtuachPrQN/wlMj5BVhBJPS0nnXDWzHp/f29k2dY8W+sSMPTO7SjCb3oc5Uji270fdX2NHvrheCKymzGoQMw==";
////    NSData *encryptedData = [NSData base64DataFromString:path];
////    path = [ZCSecureTools decryptAESData:path app_key:@"697CAF7DE44A4326931FCB5B88E35747"];
//
//
//
//   path = [ZCSecureTools AES128Decrypt:@"MquytHNRtP0ETT//1xoaIjEhapjwH7il/hvibhCmQLIlfZfcpNQ4JasuTpvJlhiFIcUVWWQyLjzyXa7mdMJrbTHu1NOimBqHEVrEQMlqQyntsqusNDO/dq4yLIf1yVJRseGJD/5HPZY4+AfHY3J544M7AvxGsbqqQwadA594gtU=" key:@"697CAF7DE44A4326931FCB5B88E35747"];
//
//
    
}

- (void)setWsksModel:(KeChengWeiShangKeShiModel *)wsksModel {
    if (wsksModel.isViewResource) {
        
    }
    NSMutableString *resource = wsksModel.isViewResource;
    if ([resource isEqualToString:@"1"]) {
         [self getKeChengXueShengLisetRequest];
    }
}

- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, APP_HEIGHT-84) style:UITableViewStyleGrouped];
        tableView.delegate  =self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor whiteColor];
        [tableView registerClass:[KeChengXueNextWoDeZiYuanViewCell class] forCellReuseIdentifier:@"KeChengXueNextWoDeZiYuanViewCell"];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _headerView = [[KeChengNextHeaderView alloc]initWithClassID:self.classID andLessonID:self.lessonId andFrame:CGRectMake(0, 0,self.frame.size.width, 131)];
        _headerView.frame = CGRectMake(0, 0,self.frame.size.width, 131);
        tableView.tableHeaderView = _headerView;
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_dataArray.count == 0) {
        return 0;
    } else {
        JIaoXueKeChengKeShiZiYuanModel *model = _dataArray[section];
        return model.dataFilesData.count;
    }
    
//    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_dataArray.count == 0) {
        return 1;
    } else {
        return _dataArray.count;
        
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 55;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KeChengXueNextWoDeZiYuanViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KeChengXueNextWoDeZiYuanViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[KeChengXueNextWoDeZiYuanViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"KeChengXueNextWoDeZiYuanViewCell"];
    }
    JIaoXueKeChengKeShiZiYuanModel *model = _dataArray[indexPath.section];
    DataFilesModel *filesModel = model.dataFilesData[indexPath.row];
    cell.titleLabel.text = [NSString stringWithFormat:@"%@",filesModel.name];
    cell.detailLabel.text = filesModel.size;
    [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:filesModel.icon]];
    if ([filesModel.suffix isEqualToString:@"mp4"]) {
        [cell.rightButton setImage:[UIImage imageNamed:@"boFang_image"] forState:UIControlStateNormal];
    } else {
        [cell.rightButton setImage:[UIImage imageNamed:@"sx_xiaozu_chakan"] forState:UIControlStateNormal];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    JIaoXueKeChengKeShiZiYuanModel *model = _dataArray[indexPath.section];
    DataFilesModel *filesModel = model.dataFilesData[indexPath.row];
    
//    NSString *path = filesModel.path;
//    path = [ZCSecureTools AES128Decrypt:@"gjoqrEbAGTTXCxq/5dYm7rxePwHLvvb9g5uVAUV/QoxR6Wxntzr6Ja219ww0istuwPJtuachPrQN/wlMj5BVhBJPS0nnXDWzHp/f29k2dY8W+sSMPTO7SjCb3oc5Uji270fdX2NHvrheCKymzGoQMw==" key:@"697CAF7DE44A4326931FCB5B88E35747"];
    
    if ([filesModel.suffix isEqualToString:@"mp4"]) {
        SYPlayerViewController *playerVC = [[SYPlayerViewController alloc] init];
        playerVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
        playerVC.urlStr = filesModel.path;
        [self.contVC presentViewController:playerVC animated:YES completion:nil];
    } else {
        SYPPTViewController *wordVC = [[SYPPTViewController alloc] init];
        wordVC.urlStr = filesModel.path;
        wordVC.nameStr = filesModel.name;
        wordVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
        [self.contVC presentViewController:wordVC animated:YES completion:nil];
    }
    
    
  //  NSLog(@"点击了cell");
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *mainView = [[UIView alloc]init];
//    JIaoXueKeChengKeShiZiYuanModel *model = _dataArray[section];
//
    if (_dataArray.count > 0) {
        [mainView addSubview:[self topviewAdd:section]];
    } else {
        
        [mainView addSubview:self.titleLab];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.left.equalTo(mainView);
        }];
        NSMutableString *resource = self.wsksModel.isViewResource;
        if ([resource intValue] == 1) {
            self.titleLab.text = @"课件资源已到期";
        }
    }
//
    return mainView;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel titleColour:[UIColor colorWithHexString:@"#666666"]
                              titleFount:15
                                pingfang:@"PingFang-SC-Regular"
                               alignment:NSTextAlignmentCenter
                               titleLine:1];
        _titleLab.text = @"请先申请资源~!";
    }
    return _titleLab;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *mainView = [[UIView alloc]init];
    return mainView;
}
- (UIView *)topviewAdd:(NSInteger)section{
    _topView = [[KCZYTopView alloc]initWithFrame:CGRectMake(40, 0, self.frame.size.width-80, 50)];
    _topView.backgroundColor = [UIColorChange colorwithHexString:@"#F8F8F8"];
    _topView.backgroundColor = [UIColorChange colorwithHexString:@"#F8F8F8"];
    JIaoXueKeChengKeShiZiYuanModel *model =_dataArray[section];
    self->_topView.titleLabel.text = [NSString stringWithFormat:@"%@  %@",model.courseName,model.lesson];
    self->_topView.detailLabel.text = [NSString stringWithFormat:@"文件:%@ 大小:%@",model.files,model.size];
    return _topView;
}

#pragma mark - 得到资源
- (void)getKeChengXueShengLisetRequest{
    NSDictionary *parama = @{
        @"attendProgressId":_attendProgressId
                             };
    [MBProgressHUD mj_showLoading];
    __weak typeof(self) weakSelf = self;
    [SYRequestAPI sy_getClassServiceLessonResource:parama success:^(id  _Nonnull responseObject){
      //  NSLog(@"教学-课程-未上课时-学生资源 %@",responseObject);
        NSArray *array = [responseObject objectForKey:@"data"];
        for (NSDictionary *dd in array) {
            JIaoXueKeChengKeShiZiYuanModel *ziYuanModel = [JIaoXueKeChengKeShiZiYuanModel yy_modelWithJSON:dd];
            ziYuanModel.dataFilesData = [NSMutableArray new];
            for (NSDictionary *dic in ziYuanModel.dataFiles) {
                DataFilesModel *mo = [DataFilesModel yy_modelWithJSON:dic];
                [ziYuanModel.dataFilesData addObject:mo];
            }
            [weakSelf.dataArray addObject:ziYuanModel];
        }
        [self.tableList reloadData];
        [MBProgressHUD mj_hideHUD];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUD];
        //[self.tableList reloadData];
    }];
}
@end
